package gapidt.analysis;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class APIDesignAnalysisResult<T> {
	
	protected T subject;
	protected List<DesignProblem<?>> designProblems = new ArrayList<DesignProblem<?>>();
	protected APIDesignAnalysisResult<?> parentAnalysisResult;
	protected List<APIDesignAnalysisResult<?>> childResults = new ArrayList<APIDesignAnalysisResult<?>>();
	
	public APIDesignAnalysisResult(T subject){
		this.subject = subject;
	}

	public T getSubject() {
		return subject;
	}

	public void setSubject(T subject) {
		this.subject = subject;
	}
	
	public int getDesignProblemsCount(){
		int count = designProblems.size();
		for(APIDesignAnalysisResult<?> result : childResults)
			count += result.getDesignProblemsCount();
		return count;
	}
	
	public List<DesignProblem<?>> getDesignProblems() {
		return designProblems;
	}

	public void setDesignProblems(List<DesignProblem<?>> designProblems) {
		this.designProblems = designProblems;
	}

	public void addDesignProblem(DesignProblem<?> designProblem){
		designProblem.setAnalysisResult(this);
		this.designProblems.add(designProblem);
	}
	
	public void addAllDesignProblems(Collection<DesignProblem<?>> designProblems){
		for(DesignProblem<?> dp : designProblems)
			dp.setAnalysisResult(this);
		this.designProblems.addAll(designProblems);
	}
	
	public List<APIDesignAnalysisResult<?>> getChildResults() {
		return childResults;
	}

	public void setChildResults(List<APIDesignAnalysisResult<?>> childResults) {
		for(APIDesignAnalysisResult<?> c : childResults)
			c.setParentAnalysisResult(this);
		this.childResults = childResults;
	}
	
	public void addChildResult(APIDesignAnalysisResult<?> result){
		result.setParentAnalysisResult(this);
		this.childResults.add(result);
	}

	public APIDesignAnalysisResult<?> getParentAnalysisResult() {
		return parentAnalysisResult;
	}

	public void setParentAnalysisResult(APIDesignAnalysisResult<?> parentAnalysisResult) {
		this.parentAnalysisResult = parentAnalysisResult;
	}
	
	public APIDesignAnalysisResult<?> getRootAnalysisResult(){
		if(parentAnalysisResult == null)
			return this;
		else
			return parentAnalysisResult.getRootAnalysisResult();
	}
	
	public boolean isRootAnalysisResult(){
		return this.parentAnalysisResult == null;
	}
	
}
